<template>
  <div class="announcement-detail">
    <van-nav-bar
      title="公告详情"
      left-arrow
      @click-left="router.back()"
    />
    
    <div class="content-wrapper">
      <div class="header">
        <h1 class="title">{{ announcement.title }}</h1>
        <div class="meta">
          <span class="time">{{ formatDate(announcement.createTime) }}</span>
          <van-tag :type="getAnnouncementTagType(announcement.type)">
            {{ getAnnouncementType(announcement.type) }}
          </van-tag>
        </div>
      </div>
      
      <div class="content" v-html="announcement.content"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast } from 'vant'
import { getAnnouncementDetail } from '../api'

const route = useRoute()
const router = useRouter()
const announcement = ref({})

// 获取公告详情
const loadAnnouncementDetail = async () => {
  try {
    const res = await getAnnouncementDetail(route.params.id)
    if (res.code === 200) {
      announcement.value = res.data
    } else {
      showToast(res.message || '获取公告详情失败')
    }
  } catch (error) {
    console.error('获取公告详情失败：', error)
    showToast('获取公告详情失败')
  }
}

// 格式化日期
const formatDate = (date) => {
  if (!date) return ''
  const d = new Date(date)
  return d.getFullYear() + '-' + 
         String(d.getMonth() + 1).padStart(2, '0') + '-' + 
         String(d.getDate()).padStart(2, '0') + ' ' +
         String(d.getHours()).padStart(2, '0') + ':' +
         String(d.getMinutes()).padStart(2, '0')
}

// 获取公告类型标签样式
const getAnnouncementTagType = (type) => {
  const typeMap = {
    system: 'primary',
    department: 'success',
    emergency: 'danger',
    maintenance: 'warning',
    policy: 'info'
  }
  return typeMap[type] || 'default'
}

// 获取公告类型文本
const getAnnouncementType = (type) => {
  const typeMap = {
    system: '系统公告',
    department: '科室通知',
    emergency: '紧急通知',
    maintenance: '维护通知',
    policy: '政策通知'
  }
  return typeMap[type] || '其他'
}

onMounted(() => {
  loadAnnouncementDetail()
})
</script>

<style lang="scss" scoped>
.announcement-detail {
  min-height: 100vh;
  background-color: #fff;
}

.content-wrapper {
  padding: 16px;
}

.header {
  margin-bottom: 20px;
  
  .title {
    font-size: 20px;
    font-weight: bold;
    color: #323233;
    margin: 0 0 12px;
  }
  
  .meta {
    display: flex;
    align-items: center;
    gap: 12px;
    color: #969799;
    font-size: 14px;
  }
}

.content {
  font-size: 16px;
  line-height: 1.6;
  color: #323233;
  
  ::v-deep(img) {
    max-width: 100%;
    height: auto;
  }
  
  ::v-deep(p) {
    margin: 1em 0;
  }
}
</style> 